//
//  HHTabBarController.m
//  oschina
//
//  Created by hjl on 16/3/30.
//  Copyright © 2016年 hjl. All rights reserved.
//

#import "HHTabBarController.h"
#import "HHNavigationController.h"
#import "HHHomeController.h"
#import "HHMeController.h"
#import "HHDiscoverController.h"
#import "HHStatusController.h"
#import "HHTabBar.h"

@interface HHTabBarController ()

@end

@implementation HHTabBarController

// 初始化
+ (void)initialize
{
    UITabBarItem *item = [UITabBarItem appearance];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    [item setTitleTextAttributes:dict forState:UIControlStateNormal];
    
    NSMutableDictionary *dictSelected = [NSMutableDictionary dictionary];
    dictSelected[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    dictSelected[NSForegroundColorAttributeName] = HHBackgroundColor;
    [item setTitleTextAttributes:dictSelected forState:UIControlStateSelected];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置控制器
    [self setupController];
    
    // 设置tabbar
    [self setupTabBar];
    
}

- (void)setupTabBar{
    HHTabBar *tabbar = [[HHTabBar alloc]init];
    [self setValue:tabbar forKey:@"tabBar"];
}

- (void)setupController{
    
    // 综合
    [self addChildViewController:[[HHHomeController alloc]init] title:@"综合" image:@"tabbar-news" selectedImage:@"tabbar-news-selected"];
    // 动弹
    [self addChildViewController:[[HHStatusController alloc]init] title:@"动弹" image:@"tabbar-tweet" selectedImage:@"tabbar-tweet-selected"];
    // 发现
    [self addChildViewController:[[HHDiscoverController alloc]initWithStyle:UITableViewStyleGrouped] title:@"发现" image:@"tabbar-discover" selectedImage:@"tabbar-discover-selected"];
    // 我
    [self addChildViewController:[[HHMeController alloc]init] title:@"我" image:@"tabbar-me" selectedImage:@"tabbar-me-selected"];
}


- (void)addChildViewController:(UIViewController *)childController title:(NSString *)title image:(NSString *)image selectedImage:(NSString *) selectedImage{
    
    childController.tabBarItem.image = [UIImage imageNamed:image];
    childController.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    childController.title = title;
    HHNavigationController *naga = [[HHNavigationController alloc] initWithRootViewController:childController];
    [self addChildViewController:naga];
}

- (void)setupChildVcName:(NSString *)childVcName title:(NSString *)title image:(NSString *)image selectedImage:(NSString *) selectedImage{
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:childVcName bundle:nil];
    UITableViewController *view = [sb instantiateInitialViewController];
    
    view.tabBarItem.image = [UIImage imageNamed:image];
    view.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    view.title = title;
    HHNavigationController *naga = [[HHNavigationController alloc] initWithRootViewController:view];
    [self addChildViewController:naga];
}

@end
